package cn.itsource.controller.app;

import cn.itsource.constants.RConst;
import cn.itsource.core.CosTemplate;
import cn.itsource.result.JSONResult;
import cn.itsource.utils.AssertUtil;
import com.qcloud.cos.model.ObjectMetadata;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Tag(name = "cos",description = "cos")
@RestController
@RequestMapping("/cos/app")
public class CosController {

    @Autowired
    private CosTemplate cosTemplate;

    @Operation(summary = "cos文件上传", description = "cos文件上传")
    @Parameter(name = "=文件", description = "文件=", required = true)
    @PostMapping("/upload")
    public JSONResult upload(@RequestParam("file") MultipartFile file) {
        AssertUtil.isFalse(file.isEmpty(), "请上传文件");
        try {
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            int i = filename.lastIndexOf(RConst.Str.DOT);
            String suffix = filename.substring(i);
            return JSONResult.success(cosTemplate.CosUpload(inputStream,suffix,new ObjectMetadata()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}